class Solution {
public:
    string answerString(string word, int numFriends) {
        if(numFriends == 1) return word;
        int n = word.size();
        int maxsize = n - numFriends + 1;
        char mod = 0;
        int pos = 0;
        for(int i = 0;i < n;i++){
            if(word[i] > mod){
                mod = word[i];
                pos = i;
            }else if(word[i] == mod){
                for(int j = 1;j < maxsize;j++){
                    if(i + j < n && word[i + j] > word[pos + j]){
                        pos = i;
                        break;
                    }else if(word[i + j] < word[pos + j]){
                        break;
                    }
                }
            }
        }
        return word.substr(pos,maxsize);
    }
};